package data.vectors;

import java.util.ArrayList;

/**
 * Describe a vector that can be modified
 * @author denoyer
 *
 */
public abstract class DoubleVectorWritable extends DoubleVector {

	/**
	 * Add a value into the vector
	 */
	public abstract void setValue(int feature,double value);

	/**
	 * Product (term to term)
	 */
	public final void product(double x)
	{
		DoubleVectorIterator it=this.getNonNullIterator();
		ArrayList<Integer> fmodif=new ArrayList<Integer>();
		
		while(it.hasNext())
		{
			it.next();
			fmodif.add(it.getCurrentFeature());
		}
		for(int i:fmodif)
			this.setValue(i,getValue(i)*x);
	}
	
	/**
	 * Add (nbre de fois qu'apparait chaque mot dans un document)
	 */
	public final void add(DoubleVector v,double scalarfactor)
	{
		DoubleVectorIterator it=v.getNonNullIterator();
		while(it.hasNext())
		{
			it.next();
			int fea=it.getCurrentFeature();
			double val=it.getCurrentValue()*scalarfactor;
			this.setValue(fea,val+getValue(fea));
		}		
	}
	
	public final void add(DoubleVector v)
	{
		//System.out.println("vect : "+v);
		DoubleVectorIterator it=v.getNonNullIterator();
		while(it.hasNext())
		{
			it.next();
			int fea=it.getCurrentFeature();
			double val=it.getCurrentValue();
			this.setValue(fea,val+getValue(fea));
		}		
	}
	/**
	 * Normalize
	 */
	public final void normalize()
	{
		product(1.0/Math.sqrt(normSquare()));
	}
	
}
